from flask import Blueprint, render_template, jsonify, redirect, url_for, session
from flask import request
from .forms import RegisterForm, PForm, checkform
from models import UserModel
from exts import db,mail
from flask_mail import Message
import string
import random
bp=Blueprint("a",__name__)
@bp.route("/register", methods=['GET', 'POST'])
def register():
    if request.method == 'GET':

        return render_template("inputphone.html")
    else:
        # 验证用户提交的邮箱和验证码是否对应且正确
        # 表单验证：flask-wtf: wtforms
        form = RegisterForm(request.form)
        if form.validate():
            session['email'] = form.email.data
            session['login']=1
            print(session.get('email'))
            return redirect(url_for("a.check"))
        if session.get('isre')==1:#已经注册
            print("yes")
            print(session.get('email'))
            session['login'] = 1
            print(session.get('isre'))
            return redirect(url_for("a.check"))
        else:
            print(form.errors)
            return redirect(url_for("a.register"))


@bp.route("/password", methods=['GET', 'POST'])
def password():
    if request.method == 'GET':
        return render_template("setpassword.html")
    else:
        # 验证用户提交的邮箱和验证码是否对应且正确
        # 表单验证：flask-wtf: wtforms
        form = PForm(request.form)
        if form.validate():
            email = session.get('email')
            password = form.m_pwd2.data
            user = UserModel(email=email, password=password)
            db.session.add(user)
            db.session.commit()
            return redirect("/jump")

        else:
            print(form.errors)
            return redirect(url_for("a.password"))

@bp.route("/end")
def end():
    return render_template("headerweb.html")

@bp.route("/check", methods=['GET', 'POST'])
def check():
    print("是否注册",session.get('isre'))
    if request.method == 'GET':
        return render_template("yanzhengma.html")
    else:
        form = checkform(request.form)
        print("检查验证码")
        if form.validate():
            print(session.get('isre'))
            if session.get('isre')==1:#已经注册
                return redirect("/jump")
            else:
                return redirect(url_for("a.password"))
        else:
            print(form.errors)
            return redirect(url_for("a.check"))
@bp.route("/jump", methods=['GET', 'POST'])
def jump():
    session['islog']=1
    if session.get('now')=="hotel":
        return redirect("/commit")
    else:
        return redirect("/")

@bp.route("/captcha/email")
def get_email_captcha():
    email = session.get('email')
    # 4/6：随机数组、字母、数组和字母的组合
    source = string.digits*4
    captcha = random.sample(source, 4)
    captcha = "".join(captcha)
    session['check']=captcha
    # I/O：Input/Output
    print(email)
    print(captcha)
    message = Message(subject="软件工程实验booking系统验证码", recipients=[email], body=f"您的验证码是:{captcha}")
    mail.send(message)

    return jsonify({"code": 200, "message": "", "data": None})

@bp.route("/logout", methods=['GET', 'POST'])
def logout():
    session['email']=None
    session['islog'] = 0
    session['isre']=0
    return redirect("/")
